<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="逆向,内核," />










<meta name="description" content="前言本文简述的是x86处理器的段寄存器相关知识。">
<meta property="og:type" content="article">
<meta property="og:title" content="保护模式：段寄存器">
<meta property="og:url" content="http://liuyun000.gitee.io/2020/04/21/%E9%80%86%E5%90%91/%E4%BF%9D%E6%8A%A4%E6%A8%A1%E5%BC%8F%EF%BC%9A%E6%AE%B5%E5%AF%84%E5%AD%98%E5%99%A8/index.html">
<meta property="og:site_name" content="liuyun的博客">
<meta property="og:description" content="前言本文简述的是x86处理器的段寄存器相关知识。">
<meta property="og:image" content="http://liuyun000.gitee.io/images/segment_register_structure.jpg">
<meta property="og:image" content="http://liuyun000.gitee.io/images/segment_descriptor_Type_field.jpg">
<meta property="og:image" content="http://liuyun000.gitee.io/images/segment_descriptor_Type_field_Extend_bit.jpg">
<meta property="og:image" content="http://liuyun000.gitee.io/images/segment_descriptor_Type_field_s0.jpg">
<meta property="og:image" content="http://liuyun000.gitee.io/images/segment_descriptor_DB.jpg">
<meta property="og:image" content="http://liuyun000.gitee.io/images/call_ret_stack_short.jpg">
<meta property="og:image" content="http://liuyun000.gitee.io/images/call_ret_stack_far_a.jpg">
<meta property="og:image" content="http://liuyun000.gitee.io/images/call_stack_far_b.jpg">
<meta property="og:image" content="http://liuyun000.gitee.io/images/ret_stack_far_b.jpg">
<meta property="og:image" content="http://liuyun000.gitee.io/images/call_gate_descriptor.jpg">
<meta property="og:image" content="http://liuyun000.gitee.io/images/interrupt_gate.jpg">
<meta property="og:image" content="http://liuyun000.gitee.io/images/trap_gate.jpg">
<meta property="og:image" content="http://liuyun000.gitee.io/images/TSS_structure.png">
<meta property="og:image" content="http://liuyun000.gitee.io/images/tr_tss.jpg">
<meta property="og:image" content="http://liuyun000.gitee.io/images/tss_descriptor.jpg">
<meta property="og:image" content="http://liuyun000.gitee.io/images/task_gate.jpg">
<meta property="article:published_time" content="2020-04-20T16:00:00.000Z">
<meta property="article:modified_time" content="2020-04-21T12:08:27.445Z">
<meta property="article:author" content="liuyun">
<meta property="article:tag" content="逆向">
<meta property="article:tag" content="内核">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://liuyun000.gitee.io/images/segment_register_structure.jpg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://liuyun000.gitee.io/2020/04/21/逆向/保护模式：段寄存器/"/>





  <title>保护模式：段寄存器 | liuyun的博客</title>
  








<meta name="generator" content="Hexo 4.2.0"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">liuyun的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/%20" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://liuyun000.gitee.io/2020/04/21/%E9%80%86%E5%90%91/%E4%BF%9D%E6%8A%A4%E6%A8%A1%E5%BC%8F%EF%BC%9A%E6%AE%B5%E5%AF%84%E5%AD%98%E5%99%A8/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="liuyun">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="liuyun的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">保护模式：段寄存器</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2020-04-21T00:00:00+08:00">
                2020-04-21
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E9%80%86%E5%90%91%E5%B7%A5%E7%A8%8B/" itemprop="url" rel="index">
                    <span itemprop="name">逆向工程</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h3><p>本文简述的是x86处理器的段寄存器相关知识。</p>
<a id="more"></a>

<h3 id="段寄存器"><a href="#段寄存器" class="headerlink" title="段寄存器"></a>段寄存器</h3><h4 id="段寄存器结构"><a href="#段寄存器结构" class="headerlink" title="段寄存器结构"></a>段寄存器结构</h4><ul>
<li><p>什么是段寄存器？有哪些？</p>
<ul>
<li><p>当我们用汇编读写一个地址时：</p>
<ul>
<li><code>mov dword ptr ds:[0x123456], eax</code>，我们真正读写的地址是：ds.base+0x123456</li>
</ul>
</li>
<li><p>ES、CS、SS、DS、FS、GS、LDTR、TR  共8个</p>
</li>
</ul>
</li>
<li><p>段寄存器结构</p>
<ul>
<li><p><img src="/images/segment_register_structure.jpg" alt="段寄存器结构"></p>
</li>
<li><pre><code class="C"><span class="class"><span class="keyword">struct</span> <span class="title">SegMent</span></span>
<span class="class">{</span>
    WORD Selector;    <span class="comment">// 16位Selector</span>
    WORD Attribute;    <span class="comment">// 16位Attribute</span>
    DWORD Base;        <span class="comment">// 32位Base</span>
    DWORD Limit;    <span class="comment">// 32位Limit</span>
};
&lt;!--￼<span class="number">0</span>--&gt;
</code></pre>
</li>
</ul>
</li>
</ul>
<h4 id="段描述符属性"><a href="#段描述符属性" class="headerlink" title="段描述符属性"></a>段描述符属性</h4><ul>
<li>P位（高四字节第15位）<ul>
<li>P = 1 段描述符有效</li>
<li>P = 0 段描述符无效</li>
</ul>
</li>
<li>段描述符与段寄存器对应的关系，G位<ul>
<li><code>Attribute</code> → 段描述符高四字节 23-8</li>
<li><code>Base</code> → <code>Base 31:24 段描述符高四字节31:24; Base 23:16 段描述符高四字节7-0; Base 15:0 段描述符低四字节31:16</code></li>
<li><code>Limit</code> → <code>段描述符高四字节19:16拼上段描述符低四字节15:0，一共是20位，设为L。段描述符G位(高四字节第23位)为0时，Limit = L；为1时，Limit = L*4k-1 = L &lt;&lt; 12-1</code></li>
</ul>
</li>
<li>S位（高四字节第12位）<ul>
<li>S = 1 代码段或数据段描述符</li>
<li>S = 0 系统段描述符</li>
</ul>
</li>
<li>Type域（高四字节8-11位）<ul>
<li>S = 1时<ul>
<li><img src="/images/segment_descriptor_Type_field.jpg" alt="Type域各位意义"></li>
<li>A 访问位，表示该位最后一次被操作系统清零后，该段是否被访问过。每当处理器将该段选择符置入某个段寄存器时，就将该位置1.</li>
<li>W是否可写</li>
<li>E扩展方向</li>
<li><img src="/images/segment_descriptor_Type_field_Extend_bit.jpg" alt="扩展方向说明"></li>
<li>A 访问位</li>
<li>R 可读位</li>
<li>C 一致位  <code>C = 1 一致代码段；C = 0 非一致代码段</code></li>
</ul>
</li>
<li>S = 0<ul>
<li><img src="/images/segment_descriptor_Type_field_s0.jpg" alt="段描述符Type域，S=0时"></li>
</ul>
</li>
</ul>
</li>
<li>D/B 域（高四字节第22位）<ul>
<li>CS段影响<ul>
<li>D = 1 默认采用32位寻址方式</li>
<li>D = 0 默认采用16位寻址方式</li>
<li>前缀67 改变寻址方式</li>
</ul>
</li>
<li>SS段影响<ul>
<li>D = 1 隐式堆栈访问指令（如： PUSH POP CALL）使用32位堆栈指针寄存器ESP</li>
<li>D = 0 隐式堆栈访问指令（如： PUSH POP CALL）使用16位堆栈指针寄存器SP</li>
</ul>
</li>
<li>向下拓展的数据段<ul>
<li>D = 1 段上限为4G</li>
<li>D = 0 段上限为64KB</li>
<li><img src="/images/segment_descriptor_DB.jpg" alt="对向下拓展的数据段"></li>
</ul>
</li>
</ul>
</li>
</ul>
<h4 id="权限检查"><a href="#权限检查" class="headerlink" title="权限检查"></a>权限检查</h4><ul>
<li><p>CPU分级：Ring 0 - 3，0权限最高，3最低。</p>
</li>
<li><p>当前程序特权级别：</p>
<ul>
<li>CPL( Current Privilege Level )：当前特权级别</li>
<li>CS和SS中存储的段选择子后2位</li>
</ul>
</li>
<li><p>DPL（Descriptor Privilege Level）描述符特权级别</p>
<ul>
<li><p>DPL存储在段描述符中（高四字节第14:13位），规定了访问该段所需要的特权级别是什么，通俗地理解：</p>
<ul>
<li>如果你想访问我，那么你应该具备什么特权</li>
<li>举例说明：<code>mov ds, ax</code> 如果AX指向地段DPL = 0，但当前程序地CPL=3，这条指定是不会成功的。</li>
</ul>
</li>
<li><p>RPL（Request Privilege Level）请求特权级别</p>
</li>
<li><p>RPL是针对段选择子而言的，每个段选择子都有自己的RPL。</p>
</li>
</ul>
</li>
<li><p>数据段权限检查</p>
<ul>
<li>参考下面的代码</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"># 假设当前程序处于0环（Ring 0）</span><br><span class="line">mov ax, 000B  # 1011 RPL &#x3D; 3</span><br><span class="line">mov ds, ax    # 假设ax指向的段描述符的DPL &#x3D; 0</span><br></pre></td></tr></table></figure>



</li>
</ul>
<ul>
<li>数据段权限检查：<strong>CPL &lt;= DPL</strong> 并且 <strong>RPL &lt;= DPL</strong> （数值上的比较），所以上述代码不能通过（因为当RPL=3而DPL=0，不满足第二个条件）</li>
<li>注意：代码段和系统段描述符中的检查方式并不一样</li>
</ul>
<h4 id="代码的跨段执行"><a href="#代码的跨段执行" class="headerlink" title="代码的跨段执行"></a>代码的跨段执行</h4><ul>
<li><p>代码间的跳转（段间跳转 非调用们之类的）</p>
<ul>
<li>段间跳转，有两种情况，即要跳转的段是一致代码段还是非一致代码段。</li>
<li>同时修改CS与EIP的指令<ul>
<li>JMP FAR / CALL FAR / RETF / INT / IRETED</li>
</ul>
</li>
<li>注意，只改变EIP的指令<ul>
<li>JMP / CALL / JCC / RET</li>
</ul>
</li>
</ul>
</li>
<li><p>代码间跳转执行流程</p>
<p><code>JMP 0x20: 0x004183D7</code> CPU如何执行这行代码</p>
<ul>
<li>段选择子拆分<ul>
<li>0x20 二进制 0000 0000 0010 0000</li>
<li>RPL = 00</li>
<li>TI = 0</li>
<li>Index = 4</li>
</ul>
</li>
<li>查表得到段描述符<ul>
<li>TI = 0 所以查GDT表</li>
<li>Index = 4 找到对应的段描述符</li>
<li>四种情况可以跳转：<strong>代码段</strong>、调用门、TSS任务段、任务门</li>
</ul>
</li>
<li>权限检查<ul>
<li>如果是非一致代码段，要求：CPL == DPL 并且 RPL &lt;= DPL</li>
<li>如果是一致代码段，要求：CPL &gt;= DPL</li>
</ul>
</li>
<li>加载段描述符<ul>
<li>通过上面的权限检查后，CPU会将段描述符加载到CS段寄存器中</li>
</ul>
</li>
<li>代码执行<ul>
<li>CPU将CS.Base + Offset 的值写入EIP，然后执行CS:EIP处的代码，段间跳转结束。</li>
</ul>
</li>
<li>总结：<ul>
<li>对于一致代码段：也就是共享的段<ul>
<li>特权级高的程序不允许访问特权级低的程序：核心态不允许访问用户态的数据</li>
<li>特权级低的程序可以访问到特权级高的数据，但特权级不会改变：用户态还是用户态</li>
</ul>
</li>
<li>对于普通代码：也就是非一致代码段<ul>
<li>只允许统计访问</li>
<li>绝对进制不同级别的访问：核心态不是用户态，用户态也不是核心态</li>
</ul>
</li>
<li>直接对代码段进行JMP或者CALL的操作，无论目标是一致代码段还是非一致代码段，CPL都不回发生改变。如果要提升CPL的权限，只能通过<strong>调用门</strong></li>
</ul>
</li>
</ul>
</li>
</ul>
<h4 id="长调用与短调用"><a href="#长调用与短调用" class="headerlink" title="长调用与短调用"></a>长调用与短调用</h4><ul>
<li>短调用<ul>
<li>格式：<code>CALL 立即数/寄存器/内存</code></li>
<li>发生改变的寄存器：ESP EIP</li>
<li><img src="/images/call_ret_stack_short.jpg" alt="短调用堆栈"></li>
</ul>
</li>
<li>长调用（跨段不提权）<ul>
<li>指令格式：<code>CALL CS:EIP</code>（EIP是废弃不用的，CS选择的必须是一个调用门）</li>
<li>发生改变的寄存器：ESP EIP CS</li>
<li><img src="/images/call_ret_stack_far_a.jpg" alt="长调用不提权堆栈"></li>
</ul>
</li>
<li>长调用（跨段并提权）<ul>
<li>指令格式：<code>CALL CS:EIP</code> （EIP是废弃不用的）</li>
<li>发生改变的寄存器：ESP EIP CS SS</li>
<li><img src="/images/call_stack_far_b.jpg" alt="长调用并提权call的调用堆栈"></li>
<li><img src="/images/ret_stack_far_b.jpg" alt="长调用并提权retf堆栈"></li>
<li>也就是堆栈切换了。</li>
</ul>
</li>
<li>总结<ul>
<li>跨段调用时，一旦有权限切换，就会切换堆栈</li>
<li>CS的权限一旦改变，SS的权限也要随着改变，CS与SS的等级必须一样</li>
<li>JMP FAR 只能跳转到同级非一致代码段，但CALL FAR可以通过调用门提权，提升CPL的权限</li>
</ul>
</li>
</ul>
<h4 id="调用门"><a href="#调用门" class="headerlink" title="调用门"></a>调用门</h4><ul>
<li>调用门执行流程<ul>
<li>指令格式：<code>CALL CS:EIP</code> （EIP是废弃的）</li>
<li>执行步骤：<ul>
<li>根据CS的值 查GDT表，找到对应的段描述符，这个描述符是一个调用门</li>
<li>在调用门描述符中存储另一个代码段的段选择子</li>
<li>选择子指向的段 段.Base + 偏移地址 就是真正要执行的地址</li>
</ul>
</li>
</ul>
</li>
<li>调用门格式<ul>
<li><img src="/images/call_gate_descriptor.jpg" alt="调用门描述符格式"></li>
</ul>
</li>
<li>Windows 没有使用调用门</li>
<li>调用门总结<ul>
<li>当通过门，权限不变的时候，只会PUSH两个值：CS、返回地址，新的CS的值由调用门绝对</li>
<li>当通过门，权限改变的时候，会PUSH四个值：SS、ESP、CS、返回地址，新的CS值由调用门决定，新的SS和ESP由TSS提供</li>
<li>通过门调用时，要执行哪行代码由调用门决定，但使用RETF返回时，由堆栈中压入的值决定，这就是说，进门时只能按指定路线走，出门时可以翻墙（只要改变堆栈里面的值可以想去哪去哪）</li>
<li>可不可以再建个门出去呢？也就是用Call，当然可以。</li>
</ul>
</li>
</ul>
<h4 id="中断门"><a href="#中断门" class="headerlink" title="中断门"></a>中断门</h4><p>Windows没有使用调用门，但是使用了中断门：系统调用、调试。</p>
<ul>
<li><p>IDT</p>
<ul>
<li><p>IDT即中断描述符表，同GDT一样，IDT也是由一些列描述符组成的，每个描述符占8个字节。但要注意的是，IDT表中的第一个元素不是NULL</p>
</li>
<li><p>windbg中查看IDT表的基址和长度：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">kd&gt; r idtr</span><br><span class="line">idtr&#x3D;92026fa0</span><br><span class="line">kd&gt; r idtl</span><br><span class="line">idtl&#x3D;000007ff</span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
<li><p>IDT表的构成</p>
<p>IDT表可以包含三种门描述符：</p>
<ul>
<li>任务门描述符</li>
<li><strong>中断门描述符</strong></li>
<li>陷阱门描述符</li>
</ul>
</li>
<li><p>中断门描述符</p>
<ul>
<li><img src="/images/interrupt_gate.jpg" alt="中断门描述符"></li>
</ul>
</li>
</ul>
<h4 id="陷阱门"><a href="#陷阱门" class="headerlink" title="陷阱门"></a>陷阱门</h4><ul>
<li>陷阱门描述符（除了高四字节第8位外和中断门相同）<ul>
<li><img src="/images/trap_gate.jpg" alt="陷阱门描述符"></li>
</ul>
</li>
<li>陷阱门和中断门的区别<ul>
<li>中断门执行时，将IF位清零（忽略可屏蔽中断），但陷阱门不会。</li>
</ul>
</li>
</ul>
<h4 id="任务段"><a href="#任务段" class="headerlink" title="任务段"></a>任务段</h4><p>在调用门、中断门与陷阱门中，一旦出现权限切换，那么就会偶堆栈的切换。而且，由于CS的CPL发生改变，也导致了SS也必须要切换。</p>
<p>切换时，会有新的ESP和SS（CS是由中断门或者调用门指定）这两个值从哪里来的呢？</p>
<p>答案：<font color='red'><strong>TSS</strong></font>（Task-state segment），任务状态段。</p>
<ul>
<li><p>TSS结构</p>
<ul>
<li>TSS是一块内存，内存结构如下所示，大小104字节</li>
<li><img src="/images/TSS_structure.png" alt="TSS结构"></li>
</ul>
</li>
<li><p>TSS的作用</p>
<ul>
<li>TSS的意义在于可以同时换掉一堆寄存器</li>
</ul>
</li>
<li><p>CPU如何找到TSS？使用TR寄存器</p>
<ul>
<li><img src="/images/tr_tss.jpg" alt="TR与TSS"></li>
</ul>
</li>
<li><p>TSS段描述符</p>
<p><img src="/images/tss_descriptor.jpg" alt="任务段描述符"></p>
</li>
<li><p>TR寄存器读写</p>
<ul>
<li>将TSS段描述符加载到TR寄存器<ul>
<li>指令：<code>LTR</code></li>
<li>用LTR指令去装载的话，仅仅是改变TR寄存器的值（96位），并没有真正改变TSS</li>
<li>LTR指令只能在系统层使用</li>
<li>加载后TSS段描述符状态位会发生改变</li>
</ul>
</li>
<li>读TR寄存器<ul>
<li>指令：<code>STR</code></li>
<li>如果用<code>STR</code>去读的话，只读了TR的16位，也就是选择子</li>
</ul>
</li>
</ul>
</li>
<li><p>修改TR寄存器</p>
<ul>
<li>在Ring 0 我们可以通过LTR指令去修改TR寄存器</li>
<li>在Ring 3 我们可以通过CALL FAR 或者 JMP FAR 指令来修改</li>
</ul>
<p><strong>JMP去访问一个代码段的时候，改变的是CS和EIP</strong></p>
<ul>
<li><code>JMP 0x48: 0x12345678</code> 如果0x48是代码段</li>
<li>执行后：CS -&gt; 0x48   EIP-&gt; 0x12345678</li>
</ul>
<p><strong>用JMP去访问一个任务段的时候：</strong></p>
<ul>
<li>如果0x48是TSS段描述符，先修改TR寄存器，再用TR.Base指向的TSS中的值修改当前的寄存器</li>
</ul>
</li>
</ul>
<h4 id="任务门"><a href="#任务门" class="headerlink" title="任务门"></a>任务门</h4><ul>
<li>中断描述符表<ul>
<li><strong>任务门描述符</strong></li>
<li>中断门描述符</li>
<li>陷阱门描述符</li>
</ul>
</li>
<li>任务门描述符<ul>
<li><img src="/images/task_gate.jpg" alt="任务门描述符"></li>
</ul>
</li>
<li>任务门执行过程<ul>
<li><code>INT N</code></li>
<li>查IDT表，找到中断门描述符</li>
<li>通过中断门描述符，查GDT表，找到任务段描述符</li>
<li>使用TSS段中的值修改寄存器</li>
<li>IRETD返回</li>
</ul>
</li>
</ul>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/%E9%80%86%E5%90%91/" rel="tag"># 逆向</a>
          
            <a href="/tags/%E5%86%85%E6%A0%B8/" rel="tag"># 内核</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2020/04/19/%E9%80%86%E5%90%91/windbg+vbox+win7%E8%99%9A%E6%8B%9F%E6%9C%BA/" rel="next" title="Windows内核调试 winbdg+vbox+win7虚拟机">
                <i class="fa fa-chevron-left"></i> Windows内核调试 winbdg+vbox+win7虚拟机
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2020/04/22/%E9%80%86%E5%90%91/windbg%20reload%E6%97%A0%E6%B3%95%E4%B8%8E%E6%9C%8D%E5%8A%A1%E5%99%A8%E5%BB%BA%E7%AB%8B%E8%BF%9E%E6%8E%A5/" rel="prev" title="windbg .reload时无法与服务器建立连接">
                windbg .reload时无法与服务器建立连接 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">liuyun</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/%20%7C%7C%20archive">
              
                  <span class="site-state-item-count">16</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">9</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">21</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#前言"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#段寄存器"><span class="nav-number">2.</span> <span class="nav-text">段寄存器</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#段寄存器结构"><span class="nav-number">2.1.</span> <span class="nav-text">段寄存器结构</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#段描述符属性"><span class="nav-number">2.2.</span> <span class="nav-text">段描述符属性</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#权限检查"><span class="nav-number">2.3.</span> <span class="nav-text">权限检查</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#代码的跨段执行"><span class="nav-number">2.4.</span> <span class="nav-text">代码的跨段执行</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#长调用与短调用"><span class="nav-number">2.5.</span> <span class="nav-text">长调用与短调用</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#调用门"><span class="nav-number">2.6.</span> <span class="nav-text">调用门</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#中断门"><span class="nav-number">2.7.</span> <span class="nav-text">中断门</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#陷阱门"><span class="nav-number">2.8.</span> <span class="nav-text">陷阱门</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#任务段"><span class="nav-number">2.9.</span> <span class="nav-text">任务段</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#任务门"><span class="nav-number">2.10.</span> <span class="nav-text">任务门</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">liuyun</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginModelPath":"assets/","model":{"jsonPath":"/live2dw/assets/miku.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":false},"log":false,"pluginJsPath":"lib/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>
